package reflect_constructor;

import java.lang.reflect.Constructor;

/**
 * @author 宋鑫宇
 * @version 1.0
 * 反射入门
 */
public class Test {
    public static void main(String[] args) throws Exception {
        //1.获取类对象
        Class c = AAA.class;
        //2.获取类中全部的构造器对象
        Constructor[] constructors = c.getConstructors();//如果构造器是私有的则不能拿到构造器对象
        for (Constructor constructor : constructors) {
            System.out.println(constructor.getName() + " " + constructor.getParameterCount());
        }
        System.out.println(c);
    }

    @org.junit.jupiter.api.Test
    @SuppressWarnings("all")
    public void BB() throws Exception {
        //3.获取全部构造器（无所谓权限）
        Class c = AAA.class;
        Constructor[] constructors = c.getDeclaredConstructors();
        for (Constructor constructor : constructors) {
            System.out.println(constructor.getName() + " " + constructor.getParameterCount());
        }
    }

    @org.junit.jupiter.api.Test
    @SuppressWarnings("all")
    public void CC() throws Exception {
        //3.获取无参构造器,私有无参构造器无法拿到
        Class c = AAA.class;
        Constructor constructor = c.getConstructor();
        System.out.println(constructor.getName() + " " + constructor.getParameterCount());
    }

    @org.junit.jupiter.api.Test
    @SuppressWarnings("all")
    public void DD() throws Exception {
        //3.获取无参构造器,(私有的也可以拿到)
        Class c = AAA.class;
        Constructor constructor = c.getDeclaredConstructor();
        System.out.println(constructor.getName() + " " + constructor.getParameterCount());
        //定位某个有参构造器
        Constructor cons1 = c.getConstructor(String.class, int.class);
        System.out.println(cons1);
    }
}
